package com.immersiondemo;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ContentFrameLayout;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import com.readystatesoftware.systembartint.SystemBarTintManager;

/**
 * 非全屏显示的activity的基类
 */
public abstract class BaseImmersionActivity extends AppCompatActivity implements ViewHelper {

    private SystemBarTintManager tintManager;
    protected int tinColor=android.R.color.holo_green_light;//状态栏的颜色
    private int targetSdkVersion=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //适配刘海屏，不适配的话会出现转发讨论旋转到横屏的时候，右边会缺一个角
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            PFullScreenUtils.setFullScreenWindowLayoutInDisplayCutout(this,getWindow());
        }
        setContentView(getLayoutId());
        initSatuBar();
     }

    private void initSatuBar() {
        PackageInfo info = null;
        try {
            info = getPackageManager().getPackageInfo(getPackageName(), 0);
            targetSdkVersion = info.applicationInfo.targetSdkVersion;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        /*
         注意 targetSdkVersion>=21时才可以去除5.0以上手机出现的遮罩层
         小于21的时候默认使用SystemBarTintManager，但是旋转横屏的时候右边会出现白色的角
         */
        Window window = getWindow();
        if (targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
            if (targetSdkVersion >= Build.VERSION_CODES.KITKAT && targetSdkVersion < Build.VERSION_CODES.LOLLIPOP) {//19<=api<21
                window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            } else if (targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {//api>=21
                window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            }
            View rootView = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
            rootView.setFitsSystemWindows(true);//给状态栏预留出空间
       } else {
                window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                // 创建状态栏的管理实例
                tintManager = new SystemBarTintManager(this);
                // 激活状态栏设置
                tintManager.setStatusBarTintEnabled(true);
                // 激活导航栏设置
                // tintManager.setNavigationBarTintEnabled(true);
                // 设置一个颜色给系统栏
//                  tintManager.setTintColor(getResources().getColor(
//                    R.color.color_00c6b4));
                tintManager.setTintResource(tinColor);
        }
    }

}
